import { ApolloServer } from "apollo-server-express";
import express from "express";
import resolvers from "./api/resolver/index";
import typeDefs from "./api/schema/index";
import { myLog } from "./log/log";
import DBinit from "./models/index";

const app = express();
const server = new ApolloServer({
    typeDefs, resolvers, tracing: false,
    context: (context) => {
        const log = myLog("graphQl req");
        log.level = "info";
        log.info(context.req.url, context.req.body);
        return { log };
    }
});

async function main() {
    const appLogger = myLog("server");
    app.use("*", (req, res, next) => {
        appLogger.info(req.url, req.body);
        next();
    });
    server.applyMiddleware({ app });
    await DBinit();
    app.listen({ port: 4000 }, () => {
        console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
    });
}

main();
